//
//  YZWorkNoteLayout.m
//  VET
//
//  Created by 刘文靖 on 2018/7/10.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZWorkNoteLayout.h"
#import "YZWorkNoteModel.h"
@interface YZWorkNoteLayout()
@property (nonatomic, assign) BOOL shouldShowMoreButton;
@end
@implementation YZWorkNoteLayout

- (instancetype)initWithNote:(YZWorkNoteModel *)note{
    if (!note) return nil;
    self = [super init];
    _note = note;
    [self layout];
    return self;
}

- (void)layout {
    [self _layout];
}


- (void)_layout {
    _marginTop = kWNCellTopMargin;
    _textHeight = 0;
    _picHeight = 0;
    _marginBottom = 0;
    
    // 文本排版，计算布局
    [self _layoutText];
    [self _layoutPics];
    // 计算高度
    _height = 0;
    _height += _marginTop;
    _height += kWNCellTopTimeHeight;
    _height += _textHeight;
    if (_shouldShowMoreButton) {
        _height += 40;
    }
    if (_picHeight > 0) {
        _height += _picHeight;
        _height += _textHeight?kWNCellPaddingPic:0;
    }
    _height += _marginBottom;
    _height += kWNCellBottomToolHeight;
}

- (void)_layoutText{
    _textHeight = 0;
    _textLayout = nil;
    
    NSMutableAttributedString *text = [self _textWithNote:_note
                                                 fontSize:kWNCellTextFontSize
                                                textColor:kWNCellTextNormalColor];
    if (text.length == 0) return;
    
    YZTextLinePositionModifier *modifier = [YZTextLinePositionModifier new];
    modifier.font = [UIFont fontWithName:@"PingFangSC-Regular" size:kWNCellTextFontSize];
    modifier.paddingTop = kWNCellPaddingContentTop;
    modifier.paddingBottom = kWNCellPaddingTextBottom;
    
    YYTextContainer *container = [YYTextContainer new];
    container.size = CGSizeMake(kWNCellContentWidth, HUGE);
    container.linePositionModifier = modifier;
    
    _textLayout = [YYTextLayout layoutWithContainer:container text:text];
    if (!_textLayout) return;
    if (_isOpening) {
        _shouldShowMoreButton = NO;
        _textHeight = [modifier heightForLineCount:_textLayout.rowCount];
    }else{
        _shouldShowMoreButton = _textLayout.rowCount > 3;
        _textHeight = [modifier heightForLineCount:MIN(_textLayout.rowCount, 3)];
    }
}

- (NSMutableAttributedString *)_textWithNote:(YZWorkNoteModel *)note
                                    fontSize:(CGFloat)fontSize
                                   textColor:(UIColor *)textColor {
    if (!note) return nil;
    
    NSMutableString *string = note.noteText.mutableCopy;
    if (string.length == 0) return nil;
    // 字体
    UIFont *font = [UIFont systemFontOfSize:fontSize];
    // 高亮状态的背景
    //    YYTextBorder *highlightBorder = [YYTextBorder new];
    //    highlightBorder.insets = UIEdgeInsetsMake(-2, 0, -2, 0);
    //    highlightBorder.cornerRadius = 3;
    //    highlightBorder.fillColor = kWBCellTextHighlightBackgroundColor;
    
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:string];
    text.font = font;
    text.color = textColor;
    
    return text;
}


- (void)_layoutPics {
    [self _layoutPicsWithNote:_note];
}

- (void)_layoutPicsWithNote:(YZWorkNoteModel *)note{
    _picSize = CGSizeZero;
    _picHeight = 0;
    if (note.attachmentList.count == 0) return;
    
    CGSize picSize = CGSizeZero;
    CGFloat picHeight = 0;
    if (note.attachType == YZWorkNoteAttachTypeAudio){//录音
        picHeight = 64;
        picSize = CGSizeMake(kScreenWidth - 60, picHeight);
    }else if (note.attachType == YZWorkNoteAttachTypeVideo){//视频
        if ([note.attachmentList.firstObject.mediaOrientation isEqualToString:@"left"] || [note.attachmentList.firstObject.mediaOrientation isEqualToString:@"right"]){//横屏
            picHeight = 188 * 375.0/kScreenWidth;
            picSize = CGSizeMake(kScreenWidth - 60, picHeight);
        }else{//竖屏
            picHeight = 175;
            picSize = CGSizeMake(113, 175);
        }
    }else{
        CGFloat len1_3 = 89;
        switch (note.attachmentList.count) {
            case 1: {//只有一张图片时 宽高设置待UI定
                picHeight = 175 * (kScreenWidth / 375.0);
                picSize = CGSizeMake(kScreenWidth - 60, picHeight);
            } break;
            case 2: case 3: {
                picSize = CGSizeMake(len1_3, len1_3);
                picHeight = len1_3;
            } break;
            case 4: case 5: case 6: {
                picSize = CGSizeMake(len1_3, len1_3);
                picHeight = len1_3 * 2 + kWNCellPaddingPic;
            } break;
            default: { // 7, 8, 9
                picSize = CGSizeMake(len1_3, len1_3);
                picHeight = len1_3 * 3 + kWNCellPaddingPic * 2;
            } break;
        }
    }
    _picSize = picSize;
    _picHeight = picHeight;
}

-(void)setIsOpening:(BOOL)isOpening{
    _isOpening = isOpening;
    [self layout];
}

@end


@implementation YZTextLinePositionModifier

- (instancetype)init {
    self = [super init];
    _lineHeightMultiple = 1.34;   // for PingFang SC
    return self;
}

- (void)modifyLines:(NSArray *)lines fromText:(NSAttributedString *)text inContainer:(YYTextContainer *)container {
    //CGFloat ascent = _font.ascender;
    CGFloat ascent = _font.pointSize * 0.86;
    
    CGFloat lineHeight = _font.pointSize * _lineHeightMultiple;
    for (YYTextLine *line in lines) {
        CGPoint position = line.position;
        position.y = _paddingTop + ascent + line.row  * lineHeight;
        line.position = position;
    }
}

- (id)copyWithZone:(NSZone *)zone {
    YZTextLinePositionModifier *one = [self.class new];
    one->_font = _font;
    one->_paddingTop = _paddingTop;
    one->_paddingBottom = _paddingBottom;
    one->_lineHeightMultiple = _lineHeightMultiple;
    return one;
}

- (CGFloat)heightForLineCount:(NSUInteger)lineCount {
    if (lineCount == 0) return 0;
    CGFloat ascent = _font.pointSize * 0.86;
    CGFloat descent = _font.pointSize * 0.14;
    CGFloat lineHeight = _font.pointSize * _lineHeightMultiple;
    return _paddingTop + _paddingBottom + ascent + descent + (lineCount - 1) * lineHeight;
}



@end
